package com.heima.behavior.kafka;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dto.FollowBehaviorDto;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class KfkListener {

    @Autowired
    ApFollowBehaviorService followBehaviorService;

    @KafkaListener(topics = {"followBehavior"})
    public void listener(ConsumerRecord<?, ?> record) {
       System.out.println(record.key() + "===========" + record.value());
       //方式一
        /*Object value = record.value();
        automaticController.autoScanByMediaNewsId(Integer.valueOf((String)value));*/

       //方式二
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record); //先判断是否为空
        if(optional.isPresent()){
            FollowBehaviorDto followBehaviorDto = JSON.parseObject(record.value().toString(), FollowBehaviorDto.class);

            followBehaviorService.follow(followBehaviorDto);
        }
    }
}